﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace XamlingCore.WP.Infrastructure.Exceptions
{
    public abstract class ExceptionTranslatorBase:IExceptionTranslator
    {
        private List<UserFriendlyException> userExceptions;

        protected UserFriendlyException GetUserFriendlyException(Exception ex)
        {
            var exceptionType = ex.GetType().ToString();
            var ue = userExceptions.Where(p => exceptionType.Contains(p.Type)).FirstOrDefault();

            if (ue != null)
            {
                return ue;
            }

            return new UserFriendlyException()
                       {
                           Code = "0000",
                           Detail = ex.ToString(),
                           Guid = System.Guid.NewGuid().ToString(),
                           Title = "Update failed",
                           MoreInfo = "The application has encountered an issue which has prevented the current operation from completing. If you continue to receive this message, please confirm you have the latest version of the TelstraOne application from the Microsoft Marketplace. If you have the latest version and this problem persists, please contact Telstra on 125 111 (or 125 8880 for pre-paid)."
                       };
        }

        protected void LoadUserExceptions(string xml)
        {
            var doc = XDocument.Parse(xml);

            var query = from exception in doc.Root.Elements("exception")
                        select new UserFriendlyException()
                                   {
                                       Code = exception.Attribute("code") != null ? exception.Attribute("code").Value : "",
                                       Detail = exception.Attribute("desc") != null ? exception.Attribute("desc").Value : "",
                                       MoreInfo = exception.Attribute("more") != null ? exception.Attribute("more").Value : "",
                                       Title = exception.Attribute("title") != null ? exception.Attribute("title").Value : "",
                                       Type = exception.Attribute("type") != null ? exception.Attribute("type").Value : ""
                                   };

            userExceptions = query.ToList();


            /*  <exception type="CouldNotLoadConfigException"
             title="Could not load application configuration"
             desc="Please check that you are in range of data"
             code="0001"/> */
        }

        public virtual UserFriendlyException TranslateException(Exception ex)
        {
            return GetUserFriendlyException(ex);
        }
    }
}
